package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Course;
import com.ljc.cjgl.entitiy.Knowledge;
import com.ljc.cjgl.form.KnowledgeForm;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.CourseService;
import com.ljc.cjgl.service.KnowledgeService;

@Controller
@RequestMapping(value="/knowledge")
public class KnowledgeController {
	@Autowired
	private KnowledgeService service;
	@Autowired
	private CourseService courseService;
	@Autowired
	private UserUtils userUtils;
	
	private Specification<Knowledge> buildSpec(KnowledgeForm form){
		HashSet<Rule> rules=new HashSet<>();
		String search = form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("title").contain(search));
			rules2.add(Rule.property("creator").contain(search));
			rules2.add(Rule.property("course.name").contain(search));
			rules.add(Rule.or(rules2));
		}
		String title = form.getTitle();
		if(StringUtils.hasText(title)) {
			rules.add(Rule.property("title").contain(title));
		}
		Integer courseId = form.getCourseId();
		if(courseId!=null) {
			rules.add(Rule.property("course.id").equal(courseId));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<Course> courses=courseService.findAll();
		map.put("courses", courses);
		return "knowledge/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,KnowledgeForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<Knowledge> specification = buildSpec(form);
		Page<Knowledge> page = service.findAll(specification, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,Integer courseId,ModelMap map) {
		Knowledge model=new Knowledge();
		if(id!=null) {
			model=service.findById(id);
		}
		List<Course> courses=courseService.findAll();
		if(courseId!=null) {
			courses.clear();
			courses.add(courseService.findById(courseId));
		}
		map.put("model",model);
		map.put("courses", courses);
		return "knowledge/edit";
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(KnowledgeForm form) {
		SysUser user=userUtils.getUser();
		String userName = user.getName();
		if(service.save(form,userName)) {
			return TableResult.bulidResult(true, "数据保存成功");
		}else {
			return TableResult.bulidResult(false,"数据保存失败");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Knowledge> knowledges) {
		if(service.delete(knowledges)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
	
	
}
